import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../widgets/load_svg_widget.dart';
import '../res/r.dart';

class FingerPrintScanDialog extends StatefulWidget {
  const FingerPrintScanDialog({super.key});

  @override
  State<FingerPrintScanDialog> createState() => _FingerPrintScanDialogState();
}

class _FingerPrintScanDialogState extends State<FingerPrintScanDialog>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();

    // 创建动画控制器
    _controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    );

    // 创建上下移动的动画
    _animation = Tween<double>(
      begin: 0.2,
      end: 1.1,
    ).animate(_controller);

    // 启动动画
    _controller.repeat();

    Future.delayed(Duration(milliseconds: Random().nextInt(2001) + 1000), () {
      _controller.stop();
      Get.back();
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: 300.w,
        height: 260.w,
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.bottomCenter,
            end: Alignment.topCenter,
            colors: [
              Color.fromRGBO(71, 59, 84, 1),
              Color.fromRGBO(171, 95, 126, 1),
            ],
          ),
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.r),
          // 添加阴影效果
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.6), // 阴影颜色及透明度
              spreadRadius: 3, // 阴影扩散范围
              blurRadius: 10, // 阴影模糊程度
              offset: Offset(0, 2), // 阴影偏移量，向下偏移2个单位
            ),
          ],
        ),
        child: Stack(
          children: [
            // 指纹图标
            Center(
              child: LoadSvgWidget(
                R.icThumbprint,
                width: 100.w,
                height: 100.w,
              ),
            ),
            // 扫描线动画
            AnimatedBuilder(
              animation: _animation,
              builder: (context, child) {
                return Positioned(
                  left: 20.w,
                  right: 20.w,
                  top: _animation.value * 200.w,
                  child: Container(
                    height: 2,
                    color: Colors.orange.withOpacity(0.6),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}
